package cn.buddha.jec.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

/**
 * @ClassName TongTypeFilter
 * @Author 友野浩二
 * @Date 2020/4/6 22:58
 * @Description 自定义过滤器(指定哪些包下的类不让spring管理)
 * @Version 1.0
 */
@Slf4j
public class TongTypeFilter implements TypeFilter {

    private static final String[] EXCLUDE_CLASS_NAMES = {"cn.buddha.jec.baomidou"};

    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        String className = classMetadata.getClassName();
        log.info("className: {}", className);
        for (String excludeClassName : EXCLUDE_CLASS_NAMES) {
            // 当className中包含某个包名时，即当前加载类是某个包下面的类，就返回true，表示该类过滤，不放到spring容器中。
            if (className.contains(excludeClassName)) {
                log.info("exclude Java file is: {}", excludeClassName);
                return true;
            }
        }
        return false;
    }
}
